package com.naiterui.ehp.bs.doctor.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.service.ITencentDetectService;
import com.naiterui.ehp.bs.doctor.service.user.IDoctorRecordService;
import com.naiterui.ehp.bs.doctor.vo.DrRecordInfoVO;
import com.naiterui.ehp.bs.doctor.vo.LicenseNewParam;
import com.naiterui.ehp.bs.doctor.vo.LicenseParam;
import com.naiterui.ehp.bs.doctor.vo.RecordNewParamVO;
import com.naiterui.ehp.bs.doctor.vo.RecordParamVO;
import com.naiterui.ehp.bs.doctor.vo.TencentFaceIdVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.io.IOException;

/**
 * 医生备案相关接口
 *
 * @author 王泽浩
 * @since 1.0.0
 */
@Validated
@RestController
@RequestMapping("ad/record/")
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.AUTH_RECORD})
public class AdRecordController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdRecordController.class);
    @Autowired
    private IDoctorRecordService doctorRecordService;
    @Autowired
    private ITencentDetectService tencentDetectService;

    /**
     * 查看医生备案信息
     *
     * @param doctorId 医生ID
     * @return
     */
    @RequestMapping("/detail")
    @ApiOperation(value = "查看医生备案信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "query")})
    public ResponseVO<DrRecordInfoVO> getRecord(Long doctorId) throws BusinessException {
        LOGGER.info("查看医生备案信息 请求参数：doctorId = [{}]", doctorId);
        if (doctorId == null) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        DrRecordInfoVO drRecordInfoVO = this.doctorRecordService.getRecord(doctorId);
        return PublicService.returnResponseVO(drRecordInfoVO);
    }


    /**
     * 医生提交备案
     *
     * @param recordParam param
     */
    @PostMapping("/apply")
    @ApiOperation(value = "医生申请备案(文件上传方式，已弃用)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({})
    public ResponseVO uploadRecord(RecordParamVO recordParam, LicenseParam licenseParam) throws BusinessException, IOException {
        LOGGER.info("医生互联网医院备案接口 请求参数：{}, licenseParam: {}", recordParam, licenseParam);
        // 参数校验
        recordParam.validate(false, null);
        // 基本参数校验
        licenseParam.validate(false);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS,
            this.doctorRecordService.uploadRecord(recordParam, licenseParam), null);
    }

    /**
     * 医生提交备案 新
     *
     * @param recordParam
     *            param
     */
    @PostMapping("/apply/new")
    @ApiOperation(value = "医生申请备案（新接口）", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({})
    public ResponseVO uploadRecordNew(@Valid RecordNewParamVO recordParam, @Valid LicenseNewParam licenseParam)
        throws BusinessException, IOException {
        LOGGER.info("新医生互联网医院备案接口 请求参数：{}, licenseParam: {}", recordParam, licenseParam);

        recordParam.validate();
        licenseParam.validate();
        this.doctorRecordService.uploadRecordNew(recordParam, licenseParam);

        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    @PostMapping("/faceid")
    @ApiOperation(value = "获取人脸识别初始化参数", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "form"),
        @ApiImplicitParam(name = "idNo", value = "身份证号", required = true, dataType = "string", paramType = "form")

    })
    public ResponseVO<TencentFaceIdVO> getFaceId(Long doctorId, String idNo) throws BusinessException {
        TencentFaceIdVO tencentFaceIdVO = tencentDetectService.getFaceId(doctorId, idNo);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, tencentFaceIdVO);
    }
}
